<template>
  <div class="container">
    <el-card class="search-container">
      <el-form
        :model="searchForm"
        size="mini"
        :inline="true"
        label-suffix="："
        ref="searchForm"
      >
        <el-form-item label="商品ID" prop="goods_id">
          <el-select
            v-model="searchForm.goods_id"
            clearable
            placeholder="请选择"
          >
            <el-option
              v-for="(item, index) in goodsList"
              :key="`goods_${index}`"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="订单ID" prop="order_id">
          <el-input v-model="searchForm.order_id" clearable></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="searchForm.status" clearable placeholder="请选择">
            <el-option
              v-for="(item, index) in statusList"
              :key="`status_${index}`"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item>
        <!-- <el-form-item label="商品类型" prop="type">
          <el-select v-model="searchForm.type" clearable placeholder="请选择">
            <el-option
              v-for="(item, index) in typeList"
              :key="`type_${index}`"
              :label="item"
              :value="index"
            ></el-option>
          </el-select>
        </el-form-item> -->
        <el-form-item>
          <el-button type="primary" @click="searchHandle">查询</el-button>
        </el-form-item>
      </el-form>
    </el-card>
    <el-card class="container-result">
      <thy-caption title="查询结果" margin>
        <el-button
          type="primary clerafix"
          @click="dialogVisible = true"
          style="float: right;"
          size="mini"
        >
          导入券码
        </el-button>
      </thy-caption>
      <el-table size="small" stripe border :data="list" v-loading="loading">
        <el-table-column prop="id" label="序号" width="60" align="center" />
        <el-table-column label="商品" width="120">
          <template slot-scope="scope">
            <span>
              {{ goodsList.find(item => item.id === scope.row.goods_id).name }}
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="ticket" label="券码" width="120" />
        <el-table-column prop="date" label="有效期" width="180" />
        <el-table-column label="状态" width="80">
          <template slot-scope="scope">
            <span v-if="scope.row.status" style="color: green;">
              已使用
            </span>
            <span v-else>
              未使用
            </span>
          </template>
        </el-table-column>
        <el-table-column prop="order_id" label="订单ID" />
        <el-table-column prop="user_id" label="用户ID" />
        <el-table-column fixed="right" label="操作" width="80" align="center">
          <template slot-scope="scope">
            <span v-if="scope.row.status">-</span>
            <el-button
              v-else
              type="text"
              size="small"
              @click="deleteHandle(scope.$index)"
              >删除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        @current-change="paginationHandle"
        :current-page="page"
        :total="total"
      >
      </el-pagination>
    </el-card>

    <el-dialog title="导入券码" :visible.sync="dialogVisible">
      <el-form
        :model="form"
        label-width="100px"
        label-suffix="："
        size="mini"
        ref="form"
      >
        <el-form-item label="商品ID" prop="goods_id" required>
          <el-select v-model="form.goods_id" clearable placeholder="请选择">
            <el-option
              v-for="(item, index) in goodsList"
              :key="`goods_${index}`"
              :label="item.name"
              :value="item.id"
            ></el-option>
          </el-select>
          <!-- <el-input v-model="searchForm.body" clearable></el-input> -->
        </el-form-item>
        <el-form-item label="券码" prop="tickets" required>
          <el-input
            type="textarea"
            autosize
            placeholder="请输入券码，并用逗号‘,’分割"
            v-model="form.tickets"
          >
          </el-input>
        </el-form-item>
        <el-form-item label="有效期" prop="date" required>
          <el-date-picker
            v-model="form.date"
            type="date"
            placeholder="选择日期"
          >
          </el-date-picker>
        </el-form-item>
        <el-form-item>
          <el-button @click="dialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="addHandle">导入</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import deepClean from 'deep-clean';
export default {
  name: 'flash-ticket',
  data() {
    return {
      loading: false,
      searchForm: {},
      form: {},
      list: [],
      total: 0,
      page: 1,
      dialogVisible: false,
      statusList: ['未使用', '已使用'],
      goodsList: []
    };
  },
  methods: {
    searchHandle() {
      this.$refs.searchForm.validate(valid => {
        if (valid) {
          this.getData();
        }
      });
    },
    getData(page = 1) {
      this.loading = true;
      this.$request
        .get('/admin/flash/ticket/list', {
          params: deepClean({
            page,
            pageSize: 10,
            ...this.searchForm
          })
        })
        .then(data => {
          this.page = page;
          this.list = data.rows;
          this.total = data.count;
        })
        .finally(() => {
          this.loading = false;
        });
    },
    addHandle() {
      this.$refs.form.validate(valid => {
        if (valid) {
          this.$request.post('/admin/flash/ticket/add', this.form).then(() => {
            this.$message.success('添加成功');
            this.dialogVisible = false;
            this.getData(this.page);
          });
        }
      });
    },
    paginationHandle(page) {
      this.getData(page);
    },
    deleteHandle(idx) {
      this.$confirm('是否继续?', '删除该条目', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(() => {
          this.$request
            .post('/admin/flash/ticket/delete', {
              id: this.list[idx].id
            })
            .then(() => {
              this.getData(this.page);
            });
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消'
          });
        });
    },
    getGoodsList() {
      return this.$request.get('/admin/flash/goods/list').then(data => {
        this.goodsList = data.filter(item => item.type === 2);
      });
    }
  },
  mounted() {
    this.getGoodsList().then(() => {
      this.getData();
    });
  }
};
</script>

<style>
</style>
